package com.sihan.framework.etm.common.util;

import com.alibaba.fastjson.JSONObject;
import com.antherd.smcrypto.sm2.Sm2;
import com.sihan.framework.etm.common.cache.LocalCache;
import com.sihan.framework.etm.entity.DevicePush;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 发送数据到检测机构
 */
@Slf4j
public class SendSafetyTools {

    public static String doPost(String url, String json, ContentType contentType) {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String res = "";

        try {
            // 创建 httppost请求
            HttpPost post = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, contentType);
            post.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(post);

            res = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    public static void sendMsg(JSONObject jsonObjeca, DevicePush devicePush) throws IOException {

        Map<String, String> headers = new HashMap<>();

        log.info("===推送检测机构设备数据："+jsonObjeca);
        headers.put("Content-Type", "application/json");

        String s = HttpUtil.httpPostJson(devicePush.getPushSite(), jsonObjeca.toJSONString(), headers);
        log.info("===推送设备信息反馈{}",s);
    }

}
